#include "stdafx.h"

#include "GameObject.h"

std::vector<GameObject*> gGameObjects;
sf::Texture* GameObject::glowTexture = 0;

GameObject::GameObject() : timer(0.f)
{
	if (!glowTexture)
	{
		glowTexture = new sf::Texture();
		glowTexture->loadFromFile("data/glow.png");
	}
	glowSprite.setTexture(*glowTexture, true);
	glowSprite.setOrigin(glowSprite.getLocalBounds().width * .5f, glowSprite.getLocalBounds().height * .5f);
	gGameObjects.push_back(this);
}

bool GameObject::CollideWithPlayer(Player* player)
{
	sf::Vector2f minV = pos - size/2.0f;
	sf::Vector2f maxV = pos + size/2.0f;

	if(type == GOT_MALE_VISIBLE)
	{
		minV.x = std::max(minV.x, gMale.pos.x - VIEW_SIZE/2.0f);
		maxV.x = std::min(maxV.x, gMale.pos.x + VIEW_SIZE/2.0f);
	} else if(type == GOT_FEMALE_VISIBLE)
	{
		minV.y = std::max(minV.y, gFemale.pos.y - VIEW_SIZE/2.0f);
		maxV.y = std::min(maxV.y, gFemale.pos.y + VIEW_SIZE/2.0f);
	}

	if(minV.x >= maxV.x) return false;
	if(minV.y >= maxV.y) return false;

	sf::Vector2f minP = player->pos - player->GetSize()/2.0f;
	sf::Vector2f maxP = player->pos + player->GetSize()/2.0f;

	if(minP.x > maxV.x) return false;
	if(minP.y > maxV.y) return false;
	if(minV.x > maxP.x) return false;
	if(minV.y > maxP.y) return false;

	return true;
}

void GameObject::Draw(sf::RenderWindow& window)
{
	glowSprite.setPosition(pos);
	window.draw(glowSprite);
}

void GameObject::Step()
{
	glowSprite.rotate(30.f * DELTA);
	
	if (timer <= 3.f)
		;
	else if (timer <= 3.25f)
		pulseScale.x = pulseScale.y = 1.f + ((timer - 3.f) * 2.f);
	else if (timer <= 3.5f)
		pulseScale.x = pulseScale.y = 1.5f - ((timer - 3.25f) * 2.f);
	else if (timer <= 3.75f)
		pulseScale.x = pulseScale.y = 1.f + ((timer - 3.5f) * 2.f);
	else if (timer <= 4.f)
		pulseScale.x = pulseScale.y = 1.5f - ((timer - 3.75f) * 2.f);
	else
		timer = .0f;

	timer += DELTA;
}